import java.net.*;
import java.io.*;
import au.edu.uow.csci213quiz.*;
import java.util.*;

public class ClientHandler implements Runnable{
	private String inputLine, outputLine;
	private PrintWriter out;
	private BufferedReader in;	
	private Socket clientSocket;
	private String userName;
	private ObjectOutputStream objectOutStream;
	private ObjectInputStream objectInStream;
	//private List<Question> questions;
	//    Question questions = new QuestionLibrary();
	
	public ClientHandler(Socket aClientSocket){
		clientSocket = aClientSocket;
		userName = null;
		//SETUP I/O STREAMS
		try{
			out = new PrintWriter(clientSocket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
		}
		catch (IOException e){
			System.err.println("couldnt open I/O streams");
			System.exit(1);
		}
		
		//SETUP QBJECT STREAMS
		try{
			objectOutStream = new ObjectOutputStream(clientSocket.getOutputStream());
			objectOutStream.flush();
			objectInStream = new ObjectInputStream(clientSocket.getInputStream());
		}
		catch(IOException e){
			System.err.println("Error! - couldn't open object I/O streams");
			System.exit(-1);
		}
	}
	
	public void setUser(String userName){
		this.userName = userName;
	}
	
	public String getUser(){
		return userName;
	}
	
	//REGISTER METHOD
	public void Register(String inputLine){
		String tokens[]=null;
		if(inputLine.startsWith("REGISTER")){
			tokens =  inputLine.split("\\ ");
			setUser(tokens[1]);
			System.out.println(getUser()+" Connected");
			out.println("OK");
		}
	}
	
	//GET QUESTION METHOD
	public void getQuestion(){
		System.out.println("Question Requested");		//STUB to check for question req
		try{
			
			objectOutStream.writeObject("test");
			out.println("OK");
			
		}
		catch(IOException e){
			System.err.println("Error! - couldn't send question object to client - "+e);
			terminateClient();
			System.exit(-1);
		}
		System.out.println("Question Sent");	
		
	}
	
	public void terminateClient(){
		//shut down thread
		//System.out.println("Server thread shutting down");
		try{
			clientSocket.close();
			out.close();
			in.close();
			objectOutStream.close();
			objectInStream.close();
		}
		catch (IOException e) {
			System.err.println("Error! - couldnt close Sockets");
			System.exit(-1);
		}
		System.out.println(userName+" Disconnected");
	}
	
	public void run(){
		//System.out.println("Client IP:" + clientSocket.getInetAddress()+" Connected"); 
		out.println("You are connected!");
		for(;;){
		
			
		
			//continually read from client
			try{
				inputLine = in.readLine();
				
			}
			catch (IOException e){
				System.err.println("Error! - reading line from client");
				terminateClient();
				System.exit(1);
			}
			
			
			if(inputLine!=null){
				//register method to check if client wants to rego
				Register(inputLine);
				
				//get question method to check if user wants a question
				if(inputLine.equals("QUESTION")){
					getQuestion();
					//questionNo++;
				}
				
				
				//if the BYE message is recieved, terminate string
				if (inputLine.equals("EXIT")){
					terminateClient();
					break;
				}
				
			//	System.out.println(inputLine);			//STUB to check input from client
			}
			
			
			
		}
		
		
		

	}

	
	
}
